//
// Created by sgyt on 2020/5/19.
//

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>

int main(int argc,char *argv[])
{
    int fd;
    int res,i;

    struct timeval timeval;
    struct pollfd event;

    char buf[100];
    char ch;


    while(1)
    {
        memset(&event,0x00, sizeof(event));
        event.fd = STDIN_FILENO;
        event.events = POLLIN;
        //开启多路复用io
        res = poll((struct pollfd *)&event,1,5000);

        if(res < 0)
        {
            perror("poll is error\n");
        }else if(res == 0){
            printf("等待超时\n");
            continue;
        }else{
            if(event.revents&POLLERR){
                perror("device is error\n");
                exit(1);
            }
            if(event.revents&POLLIN){
                memset(buf,0x00, sizeof(buf));
                i = read(STDIN_FILENO,buf,100);
                if(i<0)
                {
                    perror("read is error\n");
                }else{
                    printf("buf:[%s],size:[%d]\n",buf,i);
                }
            }

        }

    }
    return 0;
}